package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.ShopCar;
import cn.wolfcode.trip.base.mapper.ShopCarMapper;
import cn.wolfcode.trip.base.service.IShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopCarServiceImpl implements IShopCarService {
    @Autowired
    private ShopCarMapper shopCarMapper;
    @Override
    public void save(ShopCar entity) {
        Long productId = entity.getProduct().getId();
        Integer count = entity.getCount();
        //查询购物车有没有该商品
        ShopCar shopCarOld = shopCarMapper.selectByProductId(productId);
        if (shopCarOld!=null){
            //如果有,获取数量,加上新加的数量
            entity.setCount(shopCarOld.getCount()+count);
            shopCarMapper.deleteByPrimaryKey(productId);
            shopCarMapper.insert(entity);
        }else {

            shopCarMapper.insert(entity);
        }
    }

    @Override
    public List<ShopCar> getProductByUser(Long userId) {
        return shopCarMapper.selectByUser(userId);
    }

    @Override
    public void delete(Long id) {
        shopCarMapper.deleteByPrimaryKey(id);
    }
}
